/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.anniversary.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.anniversary.domain.GzmuAnnUserContacts;
import me.zhengjie.modules.anniversary.domain.Student;
import me.zhengjie.modules.anniversary.service.GzmuAnnUserContactsService;
import me.zhengjie.modules.anniversary.service.dto.GzmuAnnUserContactsQueryCriteria;
import me.zhengjie.modules.anniversary.service.dto.StudentDto;
import me.zhengjie.modules.system.service.dto.DeptDto;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/**
 * @website https://el-admin.vip
 * @author yangyao
 * @date 2020-09-20
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "校友通讯录管理")
@RequestMapping("/api/gzmuAnnUserContacts")
public class GzmuAnnUserContactsController {

    private final GzmuAnnUserContactsService gzmuAnnUserContactsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('gzmuAnnUserContacts:list')")
    public void download(HttpServletResponse response, GzmuAnnUserContactsQueryCriteria criteria) throws IOException {
        gzmuAnnUserContactsService.download(gzmuAnnUserContactsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询校友通讯录")
    @ApiOperation("查询校友通讯录")
    //@PreAuthorize("@el.check('gzmuAnnUserContacts:list')")
    @AnonymousAccess
    public ResponseEntity<Object> query(GzmuAnnUserContactsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(gzmuAnnUserContactsService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @Log("查询校友通讯录")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/query")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getContactsByName(Long myId,String name, Pageable pageable) {
        List<Map<String,Object>> students = gzmuAnnUserContactsService.findContactsByName(myId,name, pageable);
        System.out.println(students);
        return new ResponseEntity<>(students,HttpStatus.OK);
    }

    @Log("查询校友通讯录")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/apply")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getContactsApply(Long myId,Pageable pageable) {
        List<Map<String,Object>> students = gzmuAnnUserContactsService.findContactsApply(myId,pageable);
        System.out.println(students);
        return new ResponseEntity<>(students,HttpStatus.OK);
    }

    @Log("修改校友通讯录")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/alter")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> alterByContactsId(Long contactId,Integer consent) {
        gzmuAnnUserContactsService.alterByContactsId(contactId,consent);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("/createByUserId")
    @Log("新增校友通讯录")
    @ApiOperation("新增校友通讯录")
    @AnonymousAccess
    public ResponseEntity<Object> createByUserId(Long myId,Long userId){
        GzmuAnnUserContacts annUserContacts = gzmuAnnUserContactsService.findByUserId(myId,userId);
        if (annUserContacts == null){
            annUserContacts = new GzmuAnnUserContacts();
            annUserContacts.setUserContactsId(userId);
            annUserContacts.setUserId(myId);
            annUserContacts.setConsent(0);
            gzmuAnnUserContactsService.create(annUserContacts);
        }
        return new ResponseEntity<>(annUserContacts, HttpStatus.CREATED);
    }

    @Log("新增校友通讯录")
    @ApiOperation("新增校友通讯录")
    @GetMapping("/findByMyId")
    @AnonymousAccess
    public ResponseEntity<Object> findByMyId(Long myId,Long userId){
        GzmuAnnUserContacts annUserContacts = gzmuAnnUserContactsService.findByMyId(myId, userId);
        return new ResponseEntity<>(annUserContacts, HttpStatus.CREATED);
    }

    @Log("查询校友通讯录")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/getContacts")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getContacts(Long myId, Pageable pageable) {
        List<Map<String,Object>> students = gzmuAnnUserContactsService.findByMyId(myId,pageable);
        System.out.println(students);
        return new ResponseEntity<>(students,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增校友通讯录")
    @ApiOperation("新增校友通讯录")
    @PreAuthorize("@el.check('gzmuAnnUserContacts:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody GzmuAnnUserContacts resources){
        return new ResponseEntity<>(gzmuAnnUserContactsService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改校友通讯录")
    @ApiOperation("修改校友通讯录")
    //@PreAuthorize("@el.check('gzmuAnnUserContacts:edit')")
    @AnonymousAccess
    public ResponseEntity<Object> update(@Validated @RequestBody GzmuAnnUserContacts resources){
        gzmuAnnUserContactsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除校友通讯录")
    @ApiOperation("删除校友通讯录")
    @PreAuthorize("@el.check('gzmuAnnUserContacts:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        gzmuAnnUserContactsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
